package com.hongying.lingtianxiang;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class GraphicsDemo {
    public static void main(String[] args) throws IOException {
        BufferedImage bg1 = ImageIO.read(new File("bg1.png"));
        BufferedImage bg2 = ImageIO.read(new File("bg2.png"));
        BufferedImage head = ImageIO.read(new File("head.png"));
        BufferedImage code = ImageIO.read(new File("code.png"));

        // 绘制头像
        Graphics2D g1 = bg2.createGraphics();
        g1.drawImage(head, (bg2.getWidth()/15), bg2.getHeight()/9, head.getWidth(), head.getHeight(), null);

        // 绘制昵称
        g1.setFont(g1.getFont().deriveFont(Font.BOLD,40f));
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g1.setColor(new Color(66, 66, 66));
        // 设置昵称居中
        String text = "一天到晚游泳的鱼";
        FontMetrics fm = g1.getFontMetrics(g1.getFont());
        int textWidth = fm.stringWidth(text);
        g1.drawString(text, ((bg2.getWidth() - textWidth) / 2), bg2.getHeight()/5);

        // 绘制邀请规则
        g1.setFont(g1.getFont().deriveFont(35f));
        g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g1.setColor(new Color(88, 88, 88));
        text = "邀请规则";
        fm = g1.getFontMetrics(g1.getFont());
        textWidth = fm.stringWidth(text);
        g1.drawString(text, (bg2.getWidth()/15+8), (head.getHeight()*3-80));

        // 绘制虚线
        float[] dash1 = { 2f, 0f, 2f };
        BasicStroke bs1 = new BasicStroke(1,BasicStroke.CAP_BUTT,BasicStroke.JOIN_ROUND,1.0f,dash1,2f);
        g1.setStroke(bs1);
        g1.setColor(new Color(229, 229, 229));
        g1.drawLine((bg2.getWidth()-800)/2, (bg2.getHeight()-300), (bg2.getWidth()-(bg2.getWidth()-800)/2), (bg2.getHeight()-300));

        // 绘制长按识别二维码文字
        g1.setColor(new Color(88, 88, 88));
        text = "长按识别二维码";
        fm = g1.getFontMetrics(g1.getFont());
        textWidth = fm.stringWidth(text);
        g1.drawString(text, (textWidth/4), (code.getHeight()*6+30));

        // 绘制二维码图片
        System.out.println(code.getHeight());
        System.out.println(code.getWidth());
        g1.drawImage(code, (code.getWidth()*4-25), (code.getHeight()*5+45), code.getWidth()/10, code.getHeight()/10, null);

        g1.dispose();

        // 绘制图片
        Graphics2D g2 = bg1.createGraphics();
        g2.drawImage(bg2, ((bg1.getWidth() - bg2.getWidth()) / 2), ((bg1.getHeight() - bg2.getHeight()) / 2) - 70, bg2.getWidth(), bg2.getHeight(), null);
        g2.dispose();

        ImageIO.write(bg1, "png", new File("C:\\Users\\qwer\\Desktop\\a\\poster.png"));
    }
}
